package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.*;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //添加购物车
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.先判断这个菜品/套餐是否已经再购物车中存在过
        //用动态sql语句进行判断
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //根据shoppingcart查看是否存在过相关数据
        List<ShoppingCart> list = shoppingCartMapper.selectDishOrSetmeal(shoppingCart);

        if (list.size() > 0 && list != null)//有数据说明存在过,修改一下数量
        {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updata(cart);//更新数据
        } else {
            //添加数据
            //1.判断是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null)//说明是菜品
            {
                //查询菜品相关信息
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            } else//说明是套餐
            {
                SetmealVO setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            //默认数量为1创建时间为当前时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //插入数据
            shoppingCartMapper.insert(shoppingCart);
        }

        //存在过,修改数据 number+1

        //不存在,新增数据
    }

    //查看购物车
    @Override
    public List<ShoppingCart> getShoppingCart() {
        //查看购物车
        //根据userid查看
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart
                .builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.selectDishOrSetmeal(shoppingCart);
        return list;
    }

    //清空购物车
    @Override
    public void cleanShoppingCart() {
        //获取当前用户id
        Long userid = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userid);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //查询菜品/套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.selectDishOrSetmeal(shoppingCart);
        ShoppingCart cart = list.get(0);
        Integer number = cart.getNumber();
        //如果number是1的话就直接删除这个菜品/套餐
        if(number==1)
        {
            //根据用户id和菜品id/套餐id删除这个菜品/套餐
            shoppingCartMapper.deleteDishOrSetmeal(shoppingCart);
        }else
        {
            //跟新一下number数据就好了
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.updata(shoppingCart);
        }
    }


}
